<%@ page import="bog.ThreadStatus; bog.Thread" %>
<meta name="layout" content="main-bootstrap">
<g:set var="entityName" value="${message(code: 'thread.label', default: 'Thread')}"/>

<div id="show-thread" class="content scaffold-show" role="main">

    <g:hasErrors bean="${threadInstance}">
        <div class="alert alert-error">
            <ul class="errors" role="alert">
                <g:eachError bean="${threadInstance}" var="error">
                    <li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>

                </g:eachError>
            </ul>
        </div>
    </g:hasErrors>

%{--<h1><g:message code="default.show.label" args="[entityName]"/></h1>--}%
    %{--<g:if test="${flash.message}">--}%
        %{--<div class="message" role="status">${flash.message}</div>--}%
    %{--</g:if>--}%
%{--<ol class="property-list thread">--}%

    <g:if test="${threadInstance?.question}">


        <h1>
            ${threadInstance.question.title}

            <g:if test="${threadInstance.question.owner.id == session?.user?.id}">
                <g:ownerDropDown threadInstance="${threadInstance}"/>
            </g:if>
            <g:elseif test="${session?.user?.type == 'Admin'}">
                <g:adminDropDown threadInstance="${threadInstance}"/>
            </g:elseif>

        </h1>
        <h4>
            <g:if test="${threadInstance?.course}">
                <div class="fieldcontain">
                    <span id="course-label" class="property-label"><g:message code="thread.course.label"
                                                                              default="Course:"/></span>

                    <span class="property-value" aria-labelledby="course-label"><g:link controller="course" action="show"
                                                                                        id="${threadInstance?.course?.id}">${threadInstance?.course?.encodeAsHTML()}</g:link></span>

                </div>
            </g:if>
        </h4>
        <g:if test="${threadInstance?.tags}">
            <h4>
                Tags:
                <g:if test="${threadInstance?.tags}">
                    <g:each in="${threadInstance.tags}" var="t">
                        <span class="label label-info" style="margin-right: 5px;">
                            <a style="color:white; text-decoration: none;" href="${createLink(controller: 'tag', action: 'show', id: t.id)}">${t}</a>
                            </span>
                    </g:each>
                </g:if>
            </h4>
        </g:if>
        <h4>
            Creato il ${threadInstance.question.creationDate.format("d.M.yyyy")},
            alle ${threadInstance.question.creationDate.format("hh:mm:ss")}
            da
            <g:link controller="user" action="show" id="${threadInstance.question.owner?.id}">
                ${threadInstance.question.owner.username}
            </g:link>
        </h4>

    %{--<p><pre>${threadInstance.question.content}</pre></p>--}%
        <div class="row-fluid">
            <div class="span1">
                <div class="label label-inverse">
                    ${threadInstance.question.points} points
                </div>
                <div>
                    <g:if test="${session?.user && session?.user.id != threadInstance?.question?.owner?.id && threadInstance.status != bog.ThreadStatus.CLOSED}">
                        <g:link controller="question" action="upVote" params="[userId: session.user.id, questionId: threadInstance.question.id]"><i class="icon-thumbs-up"></i></g:link>
                        <span style="padding-right: 15px"></span>
                        <g:link controller="question" action="downVote" params="[userId: session.user.id, questionId: threadInstance.question.id]"><i class="icon-thumbs-down"></i></g:link>
                    </g:if>
                </div>
            </div>
            <div class="span11">
                <pre>${threadInstance.question.content.encodeAsHTML()}</pre>
            </div>
        </div>
    </g:if>

    <g:if test="${threadInstance?.answers}">
        <hr />
        <div class="fieldcontain">

            <g:each in="${threadInstance.answers}" var="a">
                <div class="row-fluid">
                    <div class="span1">
                        <div class="label label-inverse">
                            ${a.points} points
                        </div>
                        <div>
                            <g:if test="${session.user && a.owner.id != session.user.id && threadInstance.status != bog.ThreadStatus.CLOSED}">
                                <g:link controller="answer" action="upVote" params="[userId: session.user.id, answerId: a.id]"><i class="icon-thumbs-up"></i></g:link>
                                <span style="padding-right: 15px"></span>
                                <g:link controller="answer" action="downVote" params="[userId: session.user.id, answerId: a.id]"><i class="icon-thumbs-down"></i></g:link>
                            </g:if>
                        </div>
                    </div>
                    <div class="span11 alert alert-info">
                        <g:link action="show/${a.ownerId}" controller="user" style="text-decoration: underline;font-weight: bold">${a.owner.username}</g:link>, il ${a.creationDate.format("d.M.yyyy")} alle ${a.creationDate.format("hh:mm:ss")}
                        %{--<hr style="border: none; height: 1px; color: #000000"/>--}%
                        <br />
                        ${a.content.encodeAsHTML()}
                    </div>
                </div>
            </g:each>

        </div>
    </g:if>

    <g:if test="${session.user && threadInstance.status != ThreadStatus.CLOSED}">
        <g:form method="post" action="addAnswer" controller="thread" >
            <g:hiddenField name="id" value="${threadInstance?.id}" />
            <g:hiddenField name="version" value="${threadInstance?.version}" />

            <div class="fieldcontain ${hasErrors(bean: answerInstance, field: 'content', 'error')} ">

                <g:textArea name="content" class="input-block-level" value="${answerInstance?.content}"/>
            </div>

            <fieldset class="buttons">
                <g:submitButton class="btn btn-primary pull-right" name="Answer"/>
            </fieldset>
        </g:form>
    </g:if>
    <g:elseif test="${threadInstance.status == bog.ThreadStatus.CLOSED}">
        <div class="alert alert-danger">
            This thread has been closed
        </div>
    </g:elseif>
    <g:else>
        <div class="alert alert-danger">
            You must be logged in to answer or vote on this thread
        </div>
    </g:else>
</div>